var addOrEditDialog;
require(['date','validator','grid'], function () {
	var lastsel2,selLineId,selLineName,corpId,nodes,oldVrsnName,versionDialog,workFlag,getLastsel2,edirDialog,vrsnCdHtml;
		$lineParameterSetList = $("#lineParameterSetList"),
		$addSet = $("#addSet"),$saveSet = $("#saveSet"),$formData=$("#formData");

		//左侧树
		commonFun.leftTreeNodeClickObj['funRetenSpaStationSet'] = function (event, treeId, treeNode) {
			corpId = treeNode.corpId;
			$("#forAddOrEdit").val("add");
			if(treeNode.type==='LINE'){
				selLineId=treeNode.id;
				corpId=treeNode.corpId;
				selLineName=treeNode.name;
				
				//清空表单
				// $(':input','#formData')
				// 	.not(':button, :submit, :reset, :hidden ,:radio')
				// 	.val('')
				// 	.removeAttr('checked')
				// 	.removeAttr('selected');
				//默认选中通用
				// $("#STNP0001").prop("checked","checked");
				//根据线路查数据
				if(workFlag==null||workFlag==undefined){
					getSelectVrsn();

				}
			}else{
				$.messager.alert("提示","请选择一条线路查看。");
			}
		};
		//grid初始化
	    function gridInit() {
	    	debugger
			$lineParameterSetList.grid({
				url :"/operationReport/searchStationBehaviorPage.htm",
				colModel:[
					{name: "id", label: "id", key: true, hidden: true},
					{name: "vrsnName", label: "版本名称",align: "left"},
					{name: "strtusDate", label: "启用日期",align: "center"},
					{name: "stnSn", label: "站点序号",align: "right"},
					{name: "lineType", label: "方向",dict: "LIT00000",align: "left"},
					{name: "stnName", label: "站点名称",align: "left"},
					{name: "aheadStnDura", label: "越站时长(S)",align: "right",editable:true,  edittype:'text',editoptions:{size:6,maxlength:5},editrules:{required:true}},
					{name: "stayStnDura", label: "滞站时长(S)",align: "right",editable:true, edittype:'text',editoptions:{size:6,maxlength:5}, editrules:{required:true}},
					{name: "vrsnCd", label: "版本编号",hidden: true},
					{name: "stnCd", label: "站点编号",hidden: true},
					{name: "lineType", label: "lineTypeStatus",hidden: true}
				],




				onSelectRow: function(id){
					debugger
					if(id && id!==lastsel2){
						// jQuery('#lineParameterSetList').grid('saveRow',lastsel2);
						// jQuery('#lineParameterSetList').grid('editRow',id,true);
						lastsel2=id;
						$("#carId").val(id);
					}
				},
				multiselect: false,
				rowNum:1000,
				height:$(window).height()-150,
				showPager: false,
				gridComplete: function () {
					if($("#forAddOrEdit").val()=="add"){
						var overStation = $("#overStation").val().trim();
						if(overStation) {
							if (!/^\d+$/.test(overStation)) {
								overStation=10;
							}
						}
						var stayStation = $("#stayStation").val().trim();
						if(stayStation) {
							if (!/^\d+$/.test(stayStation)) {
								stayStation=90;
							}
						}
						var rowIds = jQuery("#lineParameterSetList").jqGrid('getDataIDs');
						for (var i = 0; i < rowIds.length; i ++) {
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'vrsnName', $("#versionName").val());
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'strtusDate', $("#strtusDateStationEnable").val());
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'aheadStnDura', overStation);
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'stayStnDura', stayStation);
						}

					}
				}
			});
	    	
	    }

	    //弹窗
	    function getDialog(option) {
	    	debugger
	        option = option || {};
	        if (versionDialog) {
	        	versionDialog.dialog('open');
	            if (option.title) {
	            	versionDialog.dialog('setOption', 'title', option.title);
	            }
	            return versionDialog;
	        } else {
	        	versionDialog = $('<div>').dialog({
	                title: option.title || '新增/编辑版本',
	                url: './service/setting/addFunRetenSpa.htm',
	                height: 482,
	                width: 900,
	                autoOpen: true ,
					buttons: [
						{
							text: '确定',
							css: 'mr10',
							click: function () {
								var _t = this;
								$(_t).dialog('exec', "save",function(){
									debugger
									$(_t).dialog('close');
									getSelectVrsn();

								});
							}
						},{
						text: '取消',
						css: 'btn inline-block ml5',
						click: function () {
							$(this).dialog('close');
						}
					}]
	            })
				return versionDialog;

			}
	    }
		//新增按钮
		$addSet.click(function() {
			debugger
			if(selLineId==undefined){
				$.messager.alert("提示","请选择一条路线。");
				return;
			}
			addOrEditDialog= getDialog()
			addOrEditDialog.dialog('open');
			addOrEditDialog.dialog('exec', 'getLastsel2','add', $lineParameterSetList.getRowData(),selLineId,corpId,$("#vrsnCdSelection").val(),'add',selLineName);
		});
		//批量修改弹窗
		$("#allSet").click(function(){
			if(selLineId==undefined){
				$.messager.alert("提示","请选择一条路线。");
				return;
			}
			if($("#startFlag").val()=='0'){
				$.messager.alert("提示","启用中不可编辑。");
				return;
			}
			var addOrEditDialog= getDialog()

			addOrEditDialog.dialog('exec', 'getLastsel2',$lineParameterSetList.getRowData()[0].id, $lineParameterSetList.getRowData(),selLineId,corpId,$("#vrsnCdSelection").val(),'edit',selLineName);
	   });
		$lineParameterSetList.on('keyup',':text', function () {
		        $(this).val($(this).val().replace(/[^0-9]/g, ''));
		});
	
	
		//根据版本查询线路信息
		$("#vrsnCdSelection").on('change',function(){
			$("#forAddOrEdit").val("edit");
			$("input[name=parameSet]").prop("checked", false);
			$.ajaxExt({
				url :"/operationReport/getVrsnName.htm",
				data : {"lineId":selLineId,"vrsnName":$("#vrsnCdSelection").val()}
			}).done(function(data){
				$("#beginTimeLineParameterSet").val(data[0].beginTime);
				$("#endTimeLineParameterSet").val(data[0].endTime);
				$("#"+data[0].dateType+"").prop("checked", true);
				$("#versionName").val(data[0].vrsnName);
				$("#strtusDateStationEnable").val(data[0].strtusDate);
				$("#startFlag").val(data[0].flag);
				$lineParameterSetList.grid('setGridParam', {
					page:1,
					datatype: "JSON",
					postData:{"vrsnName":$("#vrsnCdSelection").val(),"addOrEdit":$("#forAddOrEdit").val(),"lineId":selLineId}
				}).trigger("reloadGrid");
			});
		});

		//删除版本
		$("#delSet").click(function(){
			if(selLineId==undefined){
				$.messager.alert("提示","请选择一条路线。");
				return;
			}
			var strtusDate = $lineParameterSetList.getRowData()[0].strtusDate,vrsnName = $("#vrsnCdSelection").val();
			$.messager.confirm("是否删除","线路:"+selLineName+" 将删除启用日期为:"+strtusDate+" 版本名称:"+vrsnName+" 的滞站越站设置,确定删除吗?",function(r){
				if(r){
					$.ajaxExt({
						url:"/operationReport/delStationBehavior.htm",
						data:{"lineId":selLineId,"strtusDate":strtusDate,"vrsnName":vrsnName,"corpId":corpId}
					}).done(function(){
						getSelectVrsn();
					});
				}
			});
		});
	

	//根据线路查数据
	function getSelectVrsn(){
		debugger
		$("#vrsnCdSelection").html("");
		//清空表单
		$(':input','#formData')
			.not(':button, :submit, :reset, :hidden ,:radio')
			.val('')
			.removeAttr('checked')
			.removeAttr('selected');
		$.ajaxExt({
			url :"/operationReport/getVrsnName.htm",
			data : {"lineId":selLineId}
		}).done(function(data){
			if(data==null){
				//默认选中通用
				$("#forAddOrEdit").val("add");
			    $("#STNP0001").prop("checked","checked");
			    $lineParameterSetList.grid("clearGridData");
			}else{
				 vrsnCdHtml="",beginTime="",endTime="",dataType="",vrsnName="",strtusDate="";
				$(data).each(function(i,item) { 
					vrsnCdHtml+='<option value="'+item.vrsnName+'"'+(i===0?"selected":"")+'>'+item.vrsnName+'</option>';
				});
				$("#vrsnCdSelection").html(vrsnCdHtml);
				$("#beginTimeLineParameterSet").val(data[0].beginTime);
				$("#endTimeLineParameterSet").val(data[0].endTime);
				$('input[name="parameSet"]').prop('checked',false);
				$('#'+dataType).prop('checked',true);
				$("#versionName").val(data[0].vrsnName);
				oldVrsnName=data[0].vrsnName;
				$("#strtusDateStationEnable").val(data[0].strtusDate);
				if(vrsnCdHtml){
					$("#vrsnCdSelection").trigger('change');
				}
			}
		}); 
	}
	//初始化grid
    gridInit();
});